package com.haiheng.voiceandbook.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup

class MyFlowView(context: Context?, attrs: AttributeSet?) : ViewGroup(context, attrs) {

    val TAG = "MyFlowView"

    /**
     * 存所有的子View,每一个元素 就是一行
     *
     */
    var childListView = mutableListOf<List<View>>()

    /**
     * 装一行
     */
    var childLineListView = mutableListOf<View>()

    /**
     * 每一行的高
     */
    var lineHeights = mutableListOf<Int>()

    /**
     * 父亲给我的宽高
     */
    var selWidth = 0
    var selHeight = 0



    /**
     * 测量
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        selWidth = MeasureSpec.getSize(widthMeasureSpec)
        selHeight = MeasureSpec.getSize(heightMeasureSpec)
        childListView = mutableListOf<List<View>>()
        childLineListView = mutableListOf<View>()
        lineHeights = mutableListOf<Int>()
        /**
         * 孩子希望的宽高
         */
        var childNeedWidth = 0
        var childNeedHeight = 0

        /**
         * 最终确定的宽高
         */
        var finalWidth = 0
        var finalHeight = 0

        //当前行的宽度
        var currentLineWidth = 0

        //当前高度
        var currentLineHeight = 0

        //1、获取所有的子View
        for (i in 0..(childCount - 1))
        {
            val childView = getChildAt(i)
            //2、获取子view的测量规格
            val layoutParams = childView.layoutParams
            val childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, layoutParams.width)
            val childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, layoutParams.height)
            /**
             * 测量子View
             */
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec)

            /**
             * 当前行的宽
             */

            Log.e(TAG, "父亲给的宽度${selWidth} and 当前孩子的宽度:${childView.measuredWidth}" )
            currentLineWidth = currentLineWidth + childView.measuredWidth
            //未换行
            if (currentLineWidth < selWidth) {
                Log.e(TAG, "currentLineWidth = ${currentLineWidth}" )
                //装在一行里面
                childLineListView.add(childView)


            }
            //换行
            else {
                //装入当前行
                childListView.add(childLineListView)
                childLineListView = mutableListOf<View>()
                childLineListView.add(childView)
                currentLineWidth = childView.measuredWidth
            }
            if(i==(childCount - 1)){
                //如果是最后一行
                childListView.add(childLineListView)
            }


        }
        childNeedWidth = getChildNeeddWidth()
        childNeedHeight = getChildNeeddHeight()


        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) {
            finalWidth = selWidth
        } else {
            finalWidth = childNeedWidth
        }
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
            finalHeight = selHeight
        } else {
            finalHeight = childNeedHeight
        }
        setMeasuredDimension(finalWidth, finalHeight)
    }

    /**
     * 获取孩子需要的宽
     */
    private fun getChildNeeddWidth(): Int {
        val lineList = mutableListOf<Int>()
        childListView.forEach {
            // 获取当前行的和
            val lineWidth = getLineWith(it)
            lineList.add(lineWidth)
        }
        //获取所有行最长的
        var width = 0
        lineList.forEach {
            if (it > width) {
                width = it
            }
        }
        return width

    }

    /**
     * 获取当前行的和
     */
    private fun getLineWith(it: List<View>): Int {
        var width = 0
        it.forEach {
            width = width + it.measuredWidth
        }
        return width
    }

    /**
     * 获取孩子需要的高
     */
    private fun getChildNeeddHeight(): Int {
        var height = 0
        childListView.forEach {
            height = height + getMaxHeight(it)
            lineHeights.add( getMaxHeight(it))
        }
        return height
    }

    /**
     * 获取当前行最高的
     */
    private fun getMaxHeight(it: List<View>): Int {
        var maxHeight = 0
        it.forEach {
            if (it.measuredHeight > maxHeight) {
                maxHeight = it.measuredHeight
            }
        }
        return maxHeight;
    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

        //布局
        var cLeft = paddingLeft
        var cTop = paddingTop
        for (i in 0..(childListView.size - 1))
        {
            /**
             * 当前行的所有view
             */
            val lineViews = childListView.get(i)
            /**
             * 当前行的高
             */
            val lineHight = lineHeights.get(i)

            lineViews.forEach { view ->
                val left = cLeft
                val top = cTop
                val right = view.measuredWidth + left
                val bottom = view.measuredHeight + top
                view.layout(left,top,right,bottom)
                cLeft = right
            }
            cLeft = paddingLeft
            cTop  = lineHight+cTop

        }



    }

}